package httpproxy;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Event;

import httpproxy.server.DeniedItem;

public class AccessControlList
{
	Shell shell = null;
	
	private Group groupEdit;
	private Button btnSave, btnAddItem, btnRemovePart, btnEditPart;
	private Text txtServer;
	private Table tbListDeniedItems;
	private Combo cboTypes;
	private DeniedItem deniedItem = null;
		
	public AccessControlList(Shell parent)
	{
		shell = new Shell(parent);
		initComponents();
		shell.open();
		
		tbListDeniedItems.setRedraw(false);
		for(DeniedItem itm: HttpProxyMain.getACL())
		{
			TableItem item = new TableItem(tbListDeniedItems, SWT.NONE);
			item.setText(0, itm.server);
			item.setText(1, itm.type);
			item.setData(itm);
		}
		tbListDeniedItems.update();
		tbListDeniedItems.setRedraw(true);
	}
	
	private void initComponents()
	{
		FormLayout layout = new FormLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		shell.setSize(400, 600);
		shell.setText("HTTP Proxy - Verion 1.0");
		shell.setLayout(layout);
		
		groupEdit = new Group(shell, SWT.NONE);
		groupEdit.setLayout(new FormLayout());
		
		//Declare controls
		FormData fdEdit = new FormData();
		fdEdit.left = new FormAttachment(0, 5);
		fdEdit.top = new FormAttachment(0, 5);
		fdEdit.right = new FormAttachment(100, 5);
		fdEdit.bottom = new FormAttachment(100, -45);
		groupEdit.setText("Edit word");
		groupEdit.setLayoutData(fdEdit);
		
		Label lbWord = new Label(groupEdit, SWT.NONE);
		fdEdit = new FormData();
		fdEdit.left = new FormAttachment(0, 5);
		fdEdit.top = new FormAttachment(0, 10);
		lbWord.setText("Word");
		lbWord.setLayoutData(fdEdit);
		
		txtServer = new Text(groupEdit, SWT.BORDER);
		fdEdit = new FormData();
		fdEdit.width = 200;
		fdEdit.left = new FormAttachment(lbWord, 5, SWT.RIGHT);
		fdEdit.bottom = new FormAttachment(lbWord, 0, SWT.BOTTOM);
		txtServer.setLayoutData(fdEdit);
		
		Label lbType = new Label(groupEdit, SWT.NONE);
		fdEdit = new FormData();
		fdEdit.left = new FormAttachment(lbWord, 0, SWT.LEFT);
		fdEdit.top = new FormAttachment(lbWord, 10, SWT.BOTTOM);
		lbType.setText("Type");
		lbType.setLayoutData(fdEdit);
		
		//System.out.println("[EDITWORDFORM] Chay den day");
		//cboPartOfSpeed
		cboTypes = new Combo(groupEdit, SWT.DROP_DOWN | SWT.READ_ONLY);
		String[] parts = initStringTypes();	 
		cboTypes.setItems(parts);
		fdEdit = new FormData();
		fdEdit.left = new FormAttachment(txtServer, 0, SWT.LEFT);
		fdEdit.top = new FormAttachment(txtServer, 10, SWT.BOTTOM);
		fdEdit.width = 100;
		//fdEdit.right = new FormAttachment(txtWord, 0, SWT.RIGHT);
		//fdEdit.bottom = new FormAttachment(lbPart, 5, SWT.BOTTOM);
		cboTypes.setLayoutData(fdEdit);
		
		//btnRemove
		btnRemovePart = new Button(groupEdit, SWT.PUSH);
		fdEdit = new FormData();
		fdEdit.top = new FormAttachment(cboTypes, 10, SWT.BOTTOM);
		fdEdit.right = new FormAttachment(100, -10);
		btnRemovePart.setText("Remove item");
		btnRemovePart.setLayoutData(fdEdit);
		
		//btnEditItem
		btnEditPart = new Button(groupEdit, SWT.PUSH);
		fdEdit = new FormData();
		fdEdit.top = new FormAttachment(cboTypes, 10, SWT.BOTTOM);
		fdEdit.right = new FormAttachment(btnRemovePart, -5, SWT.LEFT);
		btnEditPart.setText("Edit Part");
		btnEditPart.setLayoutData(fdEdit);
		
		//btnAddPart
		fdEdit = new FormData();
		fdEdit.top = new FormAttachment(cboTypes, 10, SWT.BOTTOM);
		fdEdit.right = new FormAttachment(btnEditPart, -5, SWT.LEFT);
		btnAddItem = new Button(groupEdit, SWT.PUSH);
		btnAddItem.setText("Add Part");
		btnAddItem.setLayoutData(fdEdit);
		
		//tbListParts
		tbListDeniedItems = new Table(groupEdit, SWT.NONE);
		tbListDeniedItems.setRedraw(false);
		tbListDeniedItems.setHeaderVisible(true);
		tbListDeniedItems.setLinesVisible(true);
				
		TableColumn[] columns = new TableColumn[2];
		columns[0] = new TableColumn(tbListDeniedItems, SWT.NONE);
		columns[0].setText("Server");
		columns[0].setWidth(170);
		//columns[0].pack();
				
		columns[1] = new TableColumn(tbListDeniedItems, SWT.NONE);
		columns[1].setText("Type");
		columns[1].setWidth(170);
		//columns[1].pack();
		
		tbListDeniedItems.setRedraw(true);
				
		fdEdit = new FormData();
		fdEdit.left = new FormAttachment(0, 5);
		fdEdit.right = new FormAttachment(100, -5);
		fdEdit.top = new FormAttachment(btnAddItem, 10, SWT.BOTTOM);
		fdEdit.bottom = new FormAttachment(100, -5);
		tbListDeniedItems.setLayoutData(fdEdit);
		
		//btnSave
		btnSave = new Button(shell, SWT.PUSH);
		fdEdit = new FormData();
		fdEdit.top = new FormAttachment(groupEdit, 10, SWT.BOTTOM);
		fdEdit.right = new FormAttachment(groupEdit, -10, SWT.RIGHT);
		btnSave.setText("Save list");
		btnSave.setLayoutData(fdEdit);
		
		addActionListener();
	}
	
	private void addActionListener()
	{		
		FocusListener focusListener = new FocusListener(){
			public void focusGained(FocusEvent e)
			{
			}
			
			public void focusLost(FocusEvent e)
			{
			}
		};
		
		txtServer.addFocusListener(focusListener);
		btnSave.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event e)
			{
				
				HttpProxyMain.saveACL();
				txtServer.setText("");
			}
		});
		
		btnAddItem.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event e)
			{
				String type = cboTypes.getText();
				String server = txtServer.getText();
				
				if(deniedItem == null)
				{
					deniedItem = new DeniedItem(server, type);
				}
				
				int itemCount = tbListDeniedItems.getItemCount();
				for(int i=0; i<itemCount; i++)
				{
					TableItem tbitem = tbListDeniedItems.getItem(i);
					if( tbitem.getText(0).equals(server) && tbitem.getText(1).equals(type) )
					{
						MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
						messageBox.setText("HTTP Proxy - Item already existed");
				        messageBox.setMessage("This denied item is already existed.");
				        messageBox.open();
				        
				        return;
					}
				}
		
				addItem2Table(deniedItem);
				HttpProxyMain.addDeniedItem(deniedItem);
			}
		});
		
		
		this.btnEditPart.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event e)
			{
				TableItem[] items = tbListDeniedItems.getSelection();
				if(items == null || items.length <= 0)
					return;
				
				deniedItem = (DeniedItem)items[0].getData();
				tbListDeniedItems.remove(tbListDeniedItems.getSelectionIndex());
				items[0].dispose();
			}
		});
		
		this.btnRemovePart.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event e)
			{
				TableItem[] items = tbListDeniedItems.getSelection();
				if(items == null || items.length <= 0)
					return;
				DeniedItem ditem = (DeniedItem)items[0].getData();
				HttpProxyMain.removeDeniedItem(ditem);
				tbListDeniedItems.remove(tbListDeniedItems.getSelectionIndex());
				items[0].dispose();
			}
		});
		
		shell.addListener(SWT.Close, new Listener() {
		      public void handleEvent(Event event) {
		    	shell.dispose();
		      }
		    });
	}
	
	private String[] initStringTypes()
	{
		return (HttpProxyMain.getProperty("response.types")).split(",");
	}
	
	private void addItem2Table(DeniedItem part)
	{
		tbListDeniedItems.setRedraw(false);
		TableItem item = new TableItem(tbListDeniedItems, SWT.NONE);
		item.setText(0, part.server);
		item.setText(1, part.type);
		item.setData(part);
		tbListDeniedItems.update();
		tbListDeniedItems.setRedraw(true);
	}
}
